package com.jabo.wechatmomentsdemo.ui.activity;

import android.os.Bundle;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.jabo.wechatmomentsdemo.R;
import com.jabo.wechatmomentsdemo.app.MomentsApplication;
import com.jabo.wechatmomentsdemo.model.bean.TweetBean;
import com.jabo.wechatmomentsdemo.model.bean.UserInfo;
import com.jabo.wechatmomentsdemo.ui.adapter.MomentsListAdapter;
import com.jabo.wechatmomentsdemo.viewmodel.MainActivityViewModel;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * @anthor JZheng3
 * @Time: 2020/9/4
 */
public class MainActivity extends BaseActivity implements OnRefreshListener, OnLoadMoreListener {
    private static final String TAG = MainActivity.class.getName();
    public static final String MONENTS_LIST_DATA = "MonentsListData";
    private MainActivityViewModel mViewModel;
    private SmartRefreshLayout mSmartRefreshLayout;
    private MomentsListAdapter mMomentsListAdapter;
    private boolean mIsRefresh;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setCustomDensity(this, MomentsApplication.getInstance());

        init();
        loadData();
        onDataResponse();
    }

    private void init() {
        RecyclerView recyclerView = findViewById(R.id.layout_recycler_view);
        mSmartRefreshLayout = findViewById(R.id.layout_refresh_view);
        mMomentsListAdapter = new MomentsListAdapter();
        //init adapter
        LinearLayoutManager layoutManager = new LinearLayoutManager(this, RecyclerView.VERTICAL, false);
        recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(mMomentsListAdapter);
        //init viewModel
        ViewModelProvider.AndroidViewModelFactory instance = ViewModelProvider.AndroidViewModelFactory.getInstance(MomentsApplication.getInstance());
        mViewModel = new ViewModelProvider(this, instance).get(MainActivityViewModel.class);
        //set refresh listener
        mSmartRefreshLayout.setOnRefreshListener(this);
        mSmartRefreshLayout.setOnLoadMoreListener(this);
        mSmartRefreshLayout.setEnableLoadMore(true);
    }

    private void loadData() {
        mSmartRefreshLayout.autoRefresh();
        mIsRefresh = true;
    }

    private void onDataResponse() {
        mViewModel.getInfoLiveData().observe(this, new Observer<UserInfo>() {
            @Override
            public void onChanged(UserInfo userInfo) {
                Log.d(TAG, "onChanged: " + userInfo.getNick());
                mMomentsListAdapter.setUserInfo(userInfo);
            }
        });

        mViewModel.getListTweetBeanLiveData().observe(this, new Observer<List<TweetBean>>() {
            @Override
            public void onChanged(List<TweetBean> tweetBeans) {
                Log.e(TAG, "onChanged: " + tweetBeans.size());
                if (mIsRefresh) {
                    mMomentsListAdapter.setTweetListData(tweetBeans);
                } else {
                    mMomentsListAdapter.addTweetListData(tweetBeans);
                }
                //finish refresh view animator
                mSmartRefreshLayout.finishRefresh();
                if (mViewModel.getTweetBeansSize() <= mMomentsListAdapter.getItemCount()) {
                    mSmartRefreshLayout.finishLoadMoreWithNoMoreData();
                    Log.d(TAG, "onChanged: no more data");
                } else {
                    mSmartRefreshLayout.finishLoadMore();
                }
            }
        });
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        mViewModel.refresh();
        mIsRefresh = true;
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        mViewModel.loadMore();
        mIsRefresh = false;
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        ArrayList<TweetBean> tweetBeans = (ArrayList<TweetBean>) mMomentsListAdapter.getTweetBeans();
        outState.putSerializable(MONENTS_LIST_DATA, tweetBeans);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        if (savedInstanceState != null) {
            Serializable serializable = savedInstanceState.getSerializable(MONENTS_LIST_DATA);
            ArrayList<TweetBean> tweetBeans = (ArrayList<TweetBean>) serializable;
            assert tweetBeans != null;
            mMomentsListAdapter.setTweetListData(tweetBeans);
        }
    }
}
